package kr.co.exight.onApp;

import library.Library;
import library.Variables;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

public class ActivityDeliveryDetail extends Activity {

	/** Called when the activity is first created. */
	public static JSONObject data;
	int imageWidth;
	String url;
	WebView wv;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//arrCompanyInfo = ActivityTabOncard.getArrayData();
		setContentView(R.layout.act_baedaldetail);


		TextView tvName = (TextView)findViewById(R.id.deliveryDetail_tvName);
		TextView tvNone = (TextView)findViewById(R.id.deliveryDetail_tvNone);
		Button btnTel = (Button)findViewById(R.id.deliveryDetail_btnTel);
		wv = (WebView)findViewById(R.id.deliveryDetail_wv);

		Library.setFontNanumGothic(tvName);
		Library.setFontNanumGothic(tvNone);
		Library.setTextViewBold(tvName);
		Library.setTextViewBold(tvNone);


		btnTel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					Library.openPhoneCallIntent(ActivityDeliveryDetail.this, data.getJSONArray("phone").getString(0));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		
		try{
			tvName.setText(data.getString("name"));
			url = (String)data.getJSONArray("menu").get(0);
			
		}catch(NullPointerException e){
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if(url != null){
			showDialog(0);
			new BitmapWidthThread().start();
			wv.setVisibility(View.VISIBLE);
			wv.setWebViewClient(new WebViewClient(){
				
				@Override
				public void onPageFinished(WebView view, String url) {
					// TODO Auto-generated method stub
					super.onPageFinished(view, url);
					try{
						dismissDialog(0);
					}catch(IllegalArgumentException e){
						e.printStackTrace();
					}
				}    
			});

			final FrameLayout.LayoutParams ZOOM_PARAMS = new FrameLayout.LayoutParams(   
					ViewGroup.LayoutParams.WRAP_CONTENT,   
					ViewGroup.LayoutParams.WRAP_CONTENT,   
					Gravity.BOTTOM + Gravity.CENTER_HORIZONTAL   
					);


			wv.setFocusable(false);
			wv.getSettings().setSupportZoom(true);
			wv.getSettings().setBuiltInZoomControls(true);
/*
			FrameLayout mContentView = (FrameLayout)getWindow().getDecorView().findViewById(android.R.id.content);
			final View zoom = wv.getZoomControls();
			mContentView.removeView(zoom);
			mContentView.addView(zoom, ZOOM_PARAMS);
			zoom.setVisibility(View.GONE);
			*/
		}

	}
	
	class BitmapWidthThread extends Thread{
		@Override
		public void run() {
			imageWidth = Library.getBitmapWidthFromWeb(Variables.SERVER_URL + url);
			mHandler.sendEmptyMessage(0);
		}
	}
	
	Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			Display display = ((WindowManager) getSystemService(ActivityDeliveryDetail.WINDOW_SERVICE)).getDefaultDisplay();
			int width = display.getWidth(); 
			double scale = (double)((double)width / (double)(imageWidth + 10));
			wv.setInitialScale((int)(scale *100));
			//wv.loadUrl(url);
			wv.getSettings().setDefaultTextEncodingName("UTF-8");
			wv.loadUrl(Variables.SERVER_URL + url);
			

		};
	};
	


	@Override // 로딩바 안보이게 하기
	protected Dialog onCreateDialog(int id) {
		final Dialog dialog = new Dialog(this, R.style.Theme_Dialog);
		View view = getLayoutInflater().inflate(R.layout.dialog_progress, null);
		dialog.setContentView(view);

		dialog.setOnCancelListener(new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				finish();
			}

		});
		return dialog;

	}


	public static void setJSONData(JSONObject _data){
		data = _data;
	}
}
